<?php
namespace Kxdr\Controller;
use Think\Controller;
class IndexController extends ExtendController {
		public function index(){
			$q = I("q");
			$q = $q?(" AND `title` LIKE '%".$q."%'"):$q;
			Vendor("Page");
			$E = M("KxdrEvents");
			$total = $E->where("`sid`=".$this->sid.$q)->getField("count(`id`)");
			$PAGE = new \Page($total,10);
			$page = $PAGE->show();
			
			$CFG = M("ShopCfg");
			$curtool = $CFG->where("`sid`=".$this->sid)->getField("curtool");
			
			$EVENT = D("EventView");
			$events = $EVENT->where("`event`.`sid`=".$this->sid.$q)->order("`event`.`id` DESC")->limit($PAGE->firstRow.','.$PAGE->listRows)->getField("id,think_type,think_type_way,time_out,time_out_type,title,description,pic_url,logo_url,need_seeds,gifting_time,game_name,start_time,location,game_descrip,game_pic_url,player_name,player_descrip,event_player_descrip,is_team,game_id,player_id");
			$GFT = D("GiftView");
			foreach($events as $k=>$e){
				if($e["time_out_type"]>0){
					$events[$k]["stop_time"]=strtotime($e["start_time"])-$e["time_out"]*60;
					$events[$k]["stop_time_str"]=date("m月d日H时i分",$events[$k]["stop_time"]);
				}else{
					$events[$k]["stop_time"]=strtotime($e["start_time"])+$e["time_out"]*60;
					$events[$k]["stop_time_str"]=date("m月d日H时i分",$events[$k]["stop_time"]);
				}
				if($events[$k]["stop_time"]<time()){
					$events[$k]["status"]="stopped";
				}else{
					$events[$k]["status"]="running";
				}
				foreach(C("KXDR_THINK_WAY") as $way){
					if($e["think_type_way"]==$way["id"]){
						$events[$k]["think_type_way_name"]=$way["name"];break;
					}
				}
				$events[$k]["gifts"]=$GFT->where("`event_gift`.`event_id`=".$k." AND `event_gift`.`sid`=".$this->sid)->getField("id,event_quant,score_diff,get_type,add_time,getting_json,name,price,pic_url,gift_id");
				foreach($events[$k]["gifts"] as $gk=>$gft){
					switch($gft["score_diff"]){
						case -1:
						$events[$k]["gifts"][$gk]["cffs"]="<b>没猜中</b>";
						break;
						case 0:
						$events[$k]["gifts"][$gk]["cffs"]="完全猜中";
						break;
						default:
						$events[$k]["gifts"][$gk]["cffs"]="差".$gft["score_diff"]."分猜中";
						breka;
					}
					foreach(C("KXDR_GETWAY") as $gw){
						if($gw["id"]==$gft["get_type"] ){
							$events[$k]["gifts"][$gk]["hqfs"] = $gw["name"];break;
						}
					}
				}
			}
			$this->assign('page',$page);
			$this->assign('events',$events);
			$this->assign('curtool',$curtool);
			$this->display();
		}
		public function step1(){
			$create_new = I("creat");
			if($create_new){ //带有创建标识的清空活动ID
				session("event_id",null);
				session("title",null);
				session("start",null);
				session("pic_url",null);
				session("descrip",null);
				session("game_id",null);
				session("game_name",null);
				session("player",null);
				session("player_id",null);
				session("player_descrip",null);
				session("gifting_time",null);
				session("cffs",null);
				session("jzsj",null);
				session("jzfs",null);
				session("cyjf",null);
				session("zmc",null);
				session("event_title",null);
			}
			$this->assign('events',$events);
			$this->assign('title',session("title"));
			$this->assign('start',session("start"));
			$this->assign('pic_url',session("pic_url"));
			$this->assign('descrip',session("descrip"));
			$this->assign('game_id',session("game_id"));
			$this->assign('game_name',session("game_name"));
			$this->display();
		}
		public function step2(){
			$start = I("start");
			$pic_url = I("pic_url");
			$descrip = I("descrip");
			$game_id = I("game_id");
			$game_name = I("game_name");
			if($game_id){
				session("start",$start);
				session("pic_url",$pic_url);
				session("descrip",$descrip);
				session("game_id",$game_id);
				session("game_name",$game_name);
				$GAME = M("KxdrGames");
				$data["edit_time"]=date("Y-m-d H:i:s",time());
				$data["start_time"]=$start;
				$data["pic_url"]=$pic_url;
				$data["name"]=$game_name;
				$data["description"]=$descrip;
				$GAME->where("`id`=$game_id AND `sid`=".$this->sid)->save($data);
			}
			$this->assign('player',session("player"));
			$this->assign('player_id',session("player_id"));
			$this->assign('player_descrip',session("player_descrip"));
			$this->assign('gifting_time',session("gifting_time"));
			$this->assign('cffs',session("cffs"));
			$this->assign('jzsj',session("jzsj"));
			$this->assign('jzfs',session("jzfs"));
			$this->assign('cyjf',session("cyjf"));
			$this->assign('title',session("title"));
			$this->assign('start',session("start"));
			$this->assign('pic_url',session("pic_url"));
			$this->assign('descrip',session("descrip"));
			$this->assign('game_id',session("game_id"));
			$this->assign('event_title',session("event_title"));
			$this->assign('zmc',session("zmc"));
			$this->display();
		}
		public function step3(){
			$edit = I("edit");
			if($edit){
				
			}
			$player = I("player");
			$player_id = I("player_id");
			$player_descrip = I("descrip");
			$cffs = I("cffs")=="input"?I("savecffs"):I("cffs");
			$jzsj = I("jzsj")?I("jzsj"):0;
			$jzfs = I("jzfs")?I("jzfs"):0;
			$cyjf = I("cyjf")?I("cyjf"):0;
			$zmc = I("zmc")?I("zmc"):0;
			$gifting_time = I("gifting_time");
			$event_title = I("event_title");
			session("player",$player);
			session("player_id",$player_id);
			session("player_descrip",$player_descrip);
			session("cffs",$cffs);
			session("jzsj",$jzsj);
			session("jzfs",$jzfs);
			session("cyjf",$cyjf);
			session("zmc",$zmc);
			session("gifting_time",$gifting_time);
			session("event_title",$event_title);
			
			if(I("cffs")=="input"){
				$CFFS = M("KxdrCffs");
				$id = $CFFS->where("`sid`=".$this->sid." AND `name`='".$cffs."'")->getField("`id`");
				if(!$id){
				$data["name"]=$cffs;
				$data["sid"]=$this->sid;
				$data["game_id"]=session("game_id");
				$CFFS->add($data);
				}
			}
			
			$event["game_id"] = session("game_id");
			$event["title"] = session("event_title");
			$event["player_id"] = session("player_id"); //数据结构更新
			$event["need_seeds"] = session("cyjf");
			$event["think_type"] = session("cffs");
			$event["think_type_way"] = session("zmc");
			$event["time_out"] = session("jzsj");
			$event["time_out_type"] = session("jzfs");
			$event["pic_url"] = session("pic_url");
			$event["gifting_time"] = session("gifting_time");
			$event["description"] = session("descrip");
			$event["player_descrip"] = session("player_descrip"); //数据结构更新
			$event["edit_time"] = date("Y-m-d H:i:s");
			$event["sid"] = $this->sid;
			$event_id = session("event_id");
			$EVENT = M("KxdrEvents");
			// $EVENT_PLAYER = M("KxdrEventPlayers");
			if(!$event_id){
				// $has_event = $EVENT->where("`think_type`='".session("cffs")."' AND `title`='".session("title")."' AND `game_id`=".session("game_id"))->getField("`id`"); 
				// 当前比赛是否有标题相同、猜分相同的活动
				// 检测重复活动，本版暂不开发此功能。
				$event_id = $EVENT->add($event);//echo $EVENT->getDbError();
				session("event_id",$event_id);
			}else{
				$EVENT->where("`id`=".$event_id)->save($event);
				$EGFT=D("GiftView"); //用视图模型
				$gifts = $EGFT->where("`gift`.`sid`=".$this->sid." AND `event_gift`.`event_id`=".$event_id)->getField("id,event_quant,name,type,score_diff,get_type,getting_json",true,true);
				$gtw = C("KXDR_GETWAY");
				foreach($gifts as $k=>$g){
					foreach($gtw as $wk=>$w){
						if($w["id"]==$g["get_type"]){
							switch($wk){
								case "order_exchange" :
								$gifts[$k]["icon"] = "icon-shopping-cart";
								$json = json_decode($g["getting_json"],true);
								$json["order_exchange"]["payment"];
								$json["order_exchange"]["order_start_date"];
								$json["order_exchange"]["order_end_date"];
								$gifts[$k]["get_type_name"]=$json["order_exchange"]["payment"]."元以上".$w["name"];
								break;
								case "lottery" :
								$gifts[$k]["icon"] = "icon-star-empty";
								$json = json_decode($g["getting_json"],true);
								$json["lottery"]["jpgl"];
								$json["lottery"]["cjcs"];
								$gifts[$k]["get_type_name"]=$json["lottery"]["jpgl"]."%".$w["name"].$json["lottery"]["cjcs"]."次";
								break;
								default:
								$gifts[$k]["icon"] = "icon-ok";
								$gifts[$k]["get_type_name"]=$w["name"];
								break;
							}
							break;
						}
					}
					if($g["score_diff"]==0){
						$gifts[$k]["score_diff"]="完全猜中";
					}else{
						$gifts[$k]["score_diff"]="差".$g["score_diff"]."分猜中";
					}
				}
			}
			$this->assign('gifts',$gifts); //奖品列表
			$this->assign('cyjf',session("cyjf")); //参与积分
			$this->assign('jzsj',session("jzsj")); //截止时间
			$this->assign('jzfs',session("jzfs")); //截止方式
			$this->assign('cffs',session("cffs")); //猜分方式
			$this->assign('zmc',session("zmc")); //猜分方式，怎么猜？猜是，猜否，猜是、否，猜数据
			$this->assign('player',session("player"));
			$this->assign('player_id',session("player_id"));
			$this->assign('player_descrip',session("player_descrip"));
			$this->assign('gifting_time',session("gifting_time"));
			$this->assign('title',session("title"));
			$this->assign('start',session("start"));
			$this->assign('pic_url',session("pic_url"));
			$this->assign('descrip',session("descrip"));
			$this->assign('game_id',session("game_id"));
			$this->assign('game_name',session("game_name"));
			$this->display();
		}
		public function step4(){
			$RITEM = M("KxdrRecomItems");
			$items = $RITEM->where("`sid`=".$this->sid." AND `event_id`=".session("event_id"))->getField("num_iid,pic_url,recomment,title");
			$this->assign('items',$items); //参与积分
			$this->assign('cyjf',session("cyjf")); //参与积分
			$this->assign('jzsj',session("jzsj")); //截止时间
			$this->assign('jzfs',session("jzfs")); //截止方式
			$this->assign('cffs',session("cffs")); //猜分方式
			$this->assign('zmc',session("zmc")); //猜分方式，怎么猜？猜是，猜否，猜是、否，猜数据
			$this->assign('player',session("player"));
			$this->assign('player_descrip',session("player_descrip"));
			$this->assign('gifting_time',session("gifting_time"));
			$this->assign('title',session("title"));
			$this->assign('start',session("start"));
			$this->assign('pic_url',session("pic_url"));
			$this->assign('descrip',session("descrip"));
			$this->assign('game_id',session("game_id"));
			$this->assign('game_name',session("game_name"));
			$this->display();
		}
}